package com.hemaapp.yjnh.activity;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.util.XtomWindowSize;
import android.content.Intent;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;

/**数据修改的界面
 * @author GuiyouZhang
 * Date：2015年10月27日 下午5:24:16
 * Email:guiyouzhang@163.com
 */
public class DataModifyActivity extends BaseActivity {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private String nameExra;
	private String valueExra; // 默认值
	private EditText editText;
	private int typeExra; // 1修改昵称;2修改电话;3简介;4邮编;5价格(默认保留小数点后两位);6纯数字

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_datamodify);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {

	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {

	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {

	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {

	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		editText = (EditText) findViewById(R.id.editText);
	}

	@Override
	protected void getExras() {
		nameExra = mIntent.getStringExtra("hint_name");
		valueExra = mIntent.getStringExtra("name");
		typeExra = mIntent.getIntExtra("type", 0);
	}

	@Override
	protected void setListener() {
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.VISIBLE);
		titleText.setText("编辑" + nameExra);
		titleRight.setText("保存");
		titleRight.setOnClickListener(new SaveListener());
		if (typeExra == 1) { // 编辑昵称
			editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });
			editText.addTextChangedListener(new EditTextWatcher());
			editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
		} else if (typeExra == 2) { // 修改电话
			editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(12) });
			editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		} else if (typeExra == 3) {
			editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
//			editText.setMinimumHeight(300);
			int mScreenHei = XtomWindowSize.getHeight(mContext);
			int height = (int)(mScreenHei/3.0);
			editText.setMinimumHeight(height);
			editText.setSingleLine(false);
		}else if (typeExra == 4) {//邮编
			editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(6) });
			editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		}else if (typeExra == 5) {//价格
			editText.addTextChangedListener(new TextWatcher() {
				private boolean isChanged = false;
				private int length = 2;//小数点后的2位

				@Override
				public void onTextChanged(CharSequence s, int start, int before,
						int count) {
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
				}

				@Override
				public void afterTextChanged(Editable s) {
					if (isChanged) {// ----->如果字符未改变则返回
						return;
					}
					String str = s.toString();
					String dotBefore = "";//小数点前的整数部分
					String dotAfter = "";//小数部分
					isChanged = true;
					
					int dotIndex = str.indexOf(".");//小数点的位置
					if (dotIndex > 0) {//有小数点
						dotBefore = str.substring(0, dotIndex);
						dotAfter = str.substring(dotIndex + 1);
					}else {//没有小数点
						dotBefore = str;
					}
					//删除整数部分开头的0
					if (dotBefore.length() != 1) {
						while (dotBefore.startsWith("0")) {
							dotBefore = dotBefore.substring(1);
						}
					}
					//删除小数点后两位之后的数字
					if (dotAfter.length() > 2) {
						dotAfter = dotAfter.substring(0, 2);
					}
					//重新组合字符串
					if (dotIndex > 0) {//有小数点
						str = dotBefore+"."+dotAfter;
					}else if (dotIndex == 0) {//如果输入的内容以小数点开头，就去掉小数点
						str = "";
					}else {//没有小数点
						str = dotBefore;
					}
					editText.setText(str);
					//设置光标到最后
					editText.setSelection(editText.length());
					isChanged = false;
				}
			});
			editText.setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
		}
		editText.setHint("请输入" + nameExra);
		if (valueExra != null) {
			editText.setText(valueExra);
		}

	}

	private void setResult(String content) {
		Intent it = getIntent();
		it.putExtra("content", content);
		setResult(RESULT_OK, it);
		finish();
	}

	private class SaveListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String content = editText.getText().toString().trim();
			if (content.length() < 1) {
				showTextDialog("请输入" + nameExra);
				return;
			}
			setResult(content);
		}

	}

	private class EditTextWatcher implements TextWatcher {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (s.length() > 8) {
				XtomToastUtil.showShortToast(mContext, nameExra + "最多输入8个字");
			}
		}

		@Override
		public void afterTextChanged(Editable s) {

		}

	}

}
